package util;

import android.os.Build;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;

import com.hdd.lancity.pad.App;
import com.hdd.lancity.pad.R;

/**
 * 创建日期：18-9-8 下午12:09
 *
 * @author machong
 * @company 文件名称： ViewAnimUtil.java
 * 类说明： Animation工具类
 */
public class ViewAnimUtil {
    private int delayTime = 3000;

    private static ViewAnimUtil mViewAnimUtil;

    public static ViewAnimUtil getInstance() {
        if (mViewAnimUtil == null) {
            mViewAnimUtil = new ViewAnimUtil();
        }
        return mViewAnimUtil;
    }


    //////////////////////////旋转动画/////////////////////////////////////////////
    Animation animation = null;

    public void rotateView(View view) {
        if (null == animation) {
            animation = AnimationUtils.loadAnimation(App.getIns(), R.anim.img_animation);
            LinearInterpolator lin = new LinearInterpolator();//设置动画匀速运动
//            AccelerateInterpolator accelerateInterpolator = new AccelerateInterpolator();
            animation.setInterpolator(lin);
        }
        if (null == view)
            return;
        view.startAnimation(animation);
    }

    //////////////////隐藏和显示的动画///////////////////////////////////
    TranslateAnimation mShowAction;

    public void showViewAnim(View view) {
        if (null == mShowAction) {
            mShowAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                    Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                    -1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
            mShowAction.setDuration(delayTime);
        }
        if (null == view)
            return;
        view.setAnimation(mShowAction);
        view.setVisibility(View.VISIBLE);
    }

    TranslateAnimation mHiddenAction;

    public void hiddenViewAnim(View view) {
        if (null == mHiddenAction) {
            mHiddenAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
                    0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
                    Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                    -1.0f);
            mHiddenAction.setDuration(delayTime);
        }
        if (null == view)
            return;
        view.setAnimation(mShowAction);
        view.setVisibility(View.GONE);
    }

    public void cleanAnim(View view) {
        if (null != view) {
            view.clearAnimation();
        }
    }

    //////////////////////////平移动画/////////////////////////////////////////////
    Animation mTransanimation = null;

    public void translateView(View view) {
        if (null == animation) {
            mTransanimation = AnimationUtils.loadAnimation(App.getIns(), R.anim.tran_anim);
            LinearInterpolator lin = new LinearInterpolator();//设置动画匀速运动
//            AccelerateInterpolator accelerateInterpolator = new AccelerateInterpolator();
            mTransanimation.setInterpolator(lin);
            mTransanimation.setRepeatMode(Animation.REVERSE);
        }
        if (null == view)
            return;
        view.startAnimation(mTransanimation);
    }

    Animation animationChecking = null;

    public void rotateCheckingView(View view) {
        if (null == animationChecking) {
            animationChecking = AnimationUtils.loadAnimation(App.getIns(), R.anim.img_animation);
            LinearInterpolator lin = new LinearInterpolator();//设置动画匀速运动
//            AccelerateInterpolator accelerateInterpolator = new AccelerateInterpolator();
            animationChecking.setInterpolator(lin);
        }
        if (null == view)
            return;
        view.startAnimation(animationChecking);
    }
}
